import os
import shutil
import time
import datetime


def mkdir(path):
    # 去除首位空格
    path = path.strip()

    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print(path + ' 目录已存在')
        return False


def mycopyfile(srcfile, dstfile):
    if not os.path.isfile(srcfile):
        print
        "%s not exist!" % (srcfile)
    else:
        fpath, fname = os.path.split(dstfile)  # 分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)  # 创建路径
        shutil.copyfile(srcfile, dstfile)  # 复制文件
        print
        "copy %s -> %s" % (srcfile, dstfile)


def func_make(path_out, list_0):  # 60部位采集
    day = datetime.date.today()
    for i in range(day_num):
        for equipment in equipments:
            path = str(day) + '-' + equipment + '-jichang'
            path_0 = path_out + '\\' + path + '\\' + path
            for i in list_0:
                path_1 = path_0 + '-' + str(i)
                mkdir(path_1 + '\Data')
                mkdir(path_1 + '\Kongsao')
                mkdir(path_1 + '\detectImage')
                mkdir(path_1 + '\originImage')
                print(path_1)
                path_2 = path_1 + '\\' + path + '-' + str(i) + '.xlsx'
                mycopyfile(path_moo, path_2)
        day = day + datetime.timedelta(days=1)


def func_make_pidai(path_out, list_pidai):
    day = datetime.date.today()
    for i in range(day_num):
        for equipment in equipments:
            path = str(day) + '-' + equipment + '-jichang'
            path_0 = path_out + '\\' + path + '\\' + path
            for i in list_0:
                path_1 = path_0 + '-' + str(i) + '-pidai'
                mkdir(path_1 + '\Data')
                mkdir(path_1 + '\Kongsao')
                mkdir(path_1 + '\detectImage')
                mkdir(path_1 + '\originImage')
                print(path_1)
                path_2 = path_1 + '\\' + path + '-' + str(i) + '-pidai.xlsx'
                mycopyfile(r'..\\模板文件\皮带-5_模板.xlsx', path_2)
        day = day + datetime.timedelta(days=1)


def func_make_shobiao(path_out, list_shobiao):
    day = datetime.date.today()
    for i in range(day_num):
        for equipment in equipments:
            path = str(day) + '-' + equipment + '-jichang'
            path_0 = path_out + '\\' + path + '\\' + path
            for i in list_0:
                path_1 = path_0 + '-' + str(i) + '-shobiao'
                mkdir(path_1 + '\Data')
                mkdir(path_1 + '\Kongsao')
                mkdir(path_1 + '\detectImage')
                mkdir(path_1 + '\originImage')
                print(path_1)
                path_2 = path_1 + '\\' + path + '-' + str(i) + '-shobiao.xlsx'
                mycopyfile(r'..\\模板文件\手腕-4_模板.xlsx', path_2)
        day = day + datetime.timedelta(days=1)


# 采集模板文件
path_moo = r'../模板文件/民航60部位_模板-雷聪.xlsx'

# 输入生成的天数
day_num = 1
# 输入每天表格的数量
execl_num = 8
# 设备号
equipments = ['1#', '4#']

path_out = 'E:\work\suan_test\TAI40-II对比实验'
if __name__ == '__main__':
    # 皮带
    # 手表
    # 正常60部位采集
    list_pidai = []
    list_shobiao = list_pidai
    # list_shobiao = [11, 12]
    # list_pidai = [13, 14]
    # 12个表
    list_0 = list(set(range(1, execl_num + 1)).difference(set(list_pidai)).difference(set(list_shobiao)))

    func_make(path_out, list_0)
    # func_make_pidai(path_out,list_pidai)
    # func_make_shobiao(path_out,list_shobiao)
